package com.demo2do.boyle.client.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.demo2do.boyle.client.service.CustomerMessageService;
import com.demo2do.boyle.common.entity.customer.Customer;
import com.demo2do.boyle.common.entity.customer.message.CustomerMessage;
import com.demo2do.boyle.common.entity.customer.message.Type;
import com.demo2do.core.service.impl.GenericServiceImpl;
/**
 * 
 * @author mohanlan
 *
 */
@Service("customerMessageService")
public class CustomerMessageServiceImpl extends GenericServiceImpl<CustomerMessage> implements CustomerMessageService{

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.CustomerMessageService#lists(com.demo2do.boyle.common.entity.customer.Customer)
	 */
	@SuppressWarnings("unchecked")
	public List<CustomerMessage> list(Customer customer) {
		List<CustomerMessage> customerMessages = genericDaoSupport.searchForList("FROM CustomerMessage customerMessage WHERE disabled = false AND customerId = :customerId ORDER BY createTime DESC", "customerId", customer.getId());
		return customerMessages;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.CustomerMessageService#create(com.demo2do.boyle.common.entity.customer.Customer)
	 */
	public void create(Customer customer, String content, Type type) {
		CustomerMessage customerMessage = new CustomerMessage(customer, content, type);
		genericDaoSupport.save(customerMessage);
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.CustomerMessageService#messageCount(com.demo2do.boyle.common.entity.customer.Customer)
	 */
	public int messageCount(Customer customer) {		
		Map<String, Object> parameters = new HashMap<String, Object>(); 
		parameters.put("customerId", customer.getId());
		return genericDaoSupport.searchForInt("SELECT COUNT(1) FROM CustomerMessage customerMessage WHERE disabled = false AND readed = false AND customerId = :customerId", parameters);
	}

}
